﻿Imports System
Imports System.Xml
Imports System.Collections
Imports System.Web

''' <summary>
''' CLASE PARAMETROS, GENERA DOCUMENTO XML PARA LA COMUNICACIÓN CON LA BASE DE DATOS
''' </summary>
''' <remarks></remarks>
Public Class Parametros
#Region "PROPIEDADES"
    Private Arreglo As ArrayList
    Private NumElementos As Integer
#End Region

#Region "Constructor"
    Public Sub New()
        MyBase.New()
        Me.Arreglo = New ArrayList
        Me.NumElementos = Me.Arreglo.Count
    End Sub
#End Region

#Region "METODOS DE ACCESO"
    ''' <summary>
    ''' Agrega un nuevo Parametro
    ''' </summary>
    ''' <param name="Nombre">Nombre parametro</param>
    ''' <param name="Valor">Valor de parametro</param>
    ''' <remarks></remarks>
    Public Sub Agregar(ByVal Nombre As String, ByVal Valor As String)
        Me.Arreglo.Add(New Parametro(Nombre, Valor))
        Me.NumElementos = Me.Arreglo.Count
    End Sub

    ''' <summary>
    ''' Genera un STRING de tipo XML
    ''' </summary>
    ''' <param name="root">nombre principal de nodo</param>
    ''' <param name="iso">Iso de declaración</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GeneraDocXML(ByVal root As String, Optional ByVal iso As String = "UTF-16") As String
        If Trim(root) = "" Then root = "parametros"
        Dim objItem As Parametro
        Dim objXML As New XmlDocument
        Dim objElement As XmlElement
        'Crear Documento XML
        objXML.AppendChild(objXML.CreateElement(root))
        'Crear Nodos en Documento XML

        For Each objItem In Me.Arreglo
            objElement = objXML.CreateElement(objItem.Nombre)
            objElement.InnerText = objItem.Valor
            objXML.DocumentElement.AppendChild(objElement)
        Next

        GeneraDocXML = "<?xml version=" & Chr(34) & "1.0" & Chr(34) & " encoding=" & Chr(34) & iso & Chr(34) & "?>"
        GeneraDocXML &= objXML.InnerXml
        objXML = Nothing
        objElement = Nothing

    End Function

    ''' <summary>
    ''' Elimina todos los parametros de memoria
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub Limpiar()
        Me.Arreglo.Clear()
    End Sub

    ''' <summary>
    ''' Genera un documento XML 
    ''' </summary>
    ''' <param name="root">nombre del nodo principal</param>
    ''' <returns>XmlDocument</returns>
    ''' <remarks></remarks>
    ''' 
    Public Function GeneraXML(ByVal root As String) As XmlDocument
        Dim objItem As Parametro
        Dim objXML As New XmlDocument
        Dim objElement As XmlElement
        'Crear Documento XML
        objXML.AppendChild(objXML.CreateElement(root))
        'Crear Nodos en Documento XML
        For Each objItem In Me.Arreglo
            objElement = objXML.CreateElement(objItem.Nombre)
            objElement.InnerText = objItem.Valor
            objXML.DocumentElement.AppendChild(objElement)
        Next
        GeneraXML = objXML
        objXML = Nothing
        objElement = Nothing
    End Function

    ''' <summary>
    ''' Elimina el Objeto de memoria
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub Dispose()
        Try
            GC.SuppressFinalize(Me)
        Catch ex As Exception
        End Try
    End Sub
#End Region
End Class





